<?php

require_once dirname(__FILE__).'/../lib/profesorGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/profesorGeneratorHelper.class.php';

/**
 * profesor actions.
 *
 * @package    dnevnik
 * @subpackage profesor
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class profesorActions extends autoProfesorActions
{
    public function executeAjaxList($request)
    {
      $slog = $request->getParameter('q');
      $profesors = array();
      if($request->getParameter('skola_id'))
      {
         $skola_id = $request->getParameter('skola_id');

         $conn = Propel::getConnection();
         $query = "SELECT skola_id FROM skola WHERE id='$skola_id'";
         $statement = $conn->prepare($query);
         $statement->execute();
         $record = $statement->fetch(PDO::FETCH_OBJ);
         $p_id = $record->skola_id;
         if($p_id)
           $p_string = "or profesor.skola_id = $p_id";
         else $p_string = "";
         $slog = addslashes($slog);

         $conn = Propel::getConnection();
         $query = "SELECT profil.id, profil.prezime, profil.ime FROM profil INNER JOIN profesor on profil.id = profesor.id
                  WHERE (profesor.skola_id = $skola_id $p_string) AND
                  (profil.prezime LIKE '%$slog%' OR profil.ime LIKE '%$slog%')
                  LIMIT 10";
         $statement = $conn->prepare($query);
         $statement->execute();
         $records = $statement->fetchAll();
      }
      else
      {
          $conn = Propel::getConnection();
          $query = "SELECT profil.id, profil.prezime, profil.ime FROM profil INNER JOIN profesor on profil.id = profesor.id
                     WHERE profil.prezime LIKE '%$slog%' OR profil.ime LIKE '%$slog%'
                     LIMIT 10";
          $statement = $conn->prepare($query);
          $statement->execute();
          $records = $statement->fetchAll();
      }
      foreach($records as $record)
        $profesors[$record['id']] = $record['prezime'].' '.$record['ime'];

      return $this->renderText(json_encode($profesors));
    }

    public function executeGetPredmetProfesors($request)
    {
       $skola_id = $request->getParameter('skola_id');
       $skola_ids = DbFinder::from('Skola')->select('Id', sfModelFinder::SIMPLE)->where('SkolaId', $skola_id)->find();
       $skola_ids[] = $skola_id;

       $profesor_ids = DbFinder::from('PredmetRazred')->select('PredmetRazred.ProfesorId', sfModelFinder::SIMPLE)->leftJoin('PredmetRazred.RazredId', 'Razred.Id')->where('Razred.SkolaId', 'in', $skola_ids)->where('PredmetRazred.PredmetId', $request->getParameter('predmet_id'))->find();
       $stranis = DbFinder::from('Razred')->select('JezikProfesor', sfModelFinder::SIMPLE)->where('SkolaId', 'in', $skola_ids)->where('Jezik', $request->getParameter('predmet_id'))->find();
       $profesors = DbFinder::from('Profil')->select(array('Id', 'Prezime', 'Ime'))->where('Id', 'in', array_merge($profesor_ids, $stranis))->find();

       $sel = '<select id="profesor_id">';
       $sel .= '<option value="0">Profesor</option>';
       foreach($profesors as $profesor)
         $sel .= '<option value="'.$profesor['Id'].'">'.$profesor['Prezime'].' '.$profesor['Ime'].'</option>';
       $sel .= '</select>';

       return $this->renderText($sel);
    }

}
